package zlog

import (
	"fmt"
	"os"
	"path/filepath"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	errorSugaredLogger, infoSugaredLogger *zap.SugaredLogger

	logPath, logFileName string
	level                int
	stdout               bool
)

func InitLog(defLogPath, defLogFileName string, defLevel int, defStdout bool) {
	env := os.Getenv("APP_ENV")
	isDevelopment := env == "test" || env == "dev"

	logPath = defLogPath
	logFileName = defLogFileName
	level = defLevel
	stdout = defStdout

	// 创建 error logger
	errorConfig := zap.NewProductionConfig()
	errorConfig.OutputPaths = []string{filepath.Join(defLogPath, "error.log")}
	errorConfig.Level.SetLevel(zapcore.ErrorLevel)
	errorConfig.Development = isDevelopment
	errorConfig.Sampling = nil
	errorConfig.Encoding = "json"
	errorConfig.EncoderConfig = zap.NewProductionEncoderConfig()
	errorConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	errorConfig.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	errorConfig.EncoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
	errorConfig.EncoderConfig.EncodeCaller = zapcore.FullCallerEncoder
	errorConfig.EncoderConfig.MessageKey = "msg"
	errorConfig.EncoderConfig.TimeKey = "timestamp"
	errorConfig.EncoderConfig.SkipLineEnding = false
	errorConfig.DisableStacktrace = true
	errorConfig.DisableCaller = true
	errorConfig.DisableStacktrace = true
	errorConfig.DisableStacktrace = true
	errorConfig.InitialFields = map[string]interface{}{ // 初始化字段
		"app": "goweb",
		"pid": os.Getpid(),
	}
	errorLogger, err := errorConfig.Build()
	if err != nil {
		fmt.Println("error logger init error : ", err.Error())
		panic(err)
	}
	errorSugaredLogger = errorLogger.Sugar()
	defer func(errorLogger *zap.Logger) {
		_ = errorLogger.Sync()
	}(errorLogger)

	// 创建 info logger
	infoConfig := zap.NewProductionConfig()
	infoConfig.OutputPaths = []string{filepath.Join(defLogPath, "info.log")}
	infoConfig.Level.SetLevel(zapcore.InfoLevel)
	infoConfig.Development = isDevelopment
	infoConfig.Sampling = nil
	infoConfig.Encoding = "json"
	infoConfig.EncoderConfig = zap.NewProductionEncoderConfig()
	infoConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	infoConfig.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	infoConfig.EncoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
	infoConfig.EncoderConfig.EncodeCaller = zapcore.FullCallerEncoder
	infoConfig.EncoderConfig.MessageKey = "msg"
	infoConfig.EncoderConfig.TimeKey = "timestamp"
	infoConfig.EncoderConfig.SkipLineEnding = false
	infoConfig.DisableStacktrace = true
	infoConfig.DisableCaller = true
	infoConfig.DisableStacktrace = true
	infoConfig.DisableStacktrace = true
	infoConfig.InitialFields = map[string]interface{}{ // 初始化字段
		"app": "goweb",
		"pid": os.Getpid(),
	}
	infoLogger, err := infoConfig.Build()
	if err != nil {
		fmt.Println("info logger init error : ", err.Error())
		panic(err)
	}
	infoSugaredLogger = infoLogger.Sugar()
	defer func(infoLogger *zap.Logger) {
		_ = infoLogger.Sync()
	}(infoLogger)
}
